//
//  DataGridCell.m
//  yilutongxing
//
//  Created by T&J on 2017/7/11.
//  Copyright © 2017年 YZE. All rights reserved.
//

#import "DataGridCell.h"
#import "UIView+Grid.h"

@implementation DataRef
@end

@interface DataGridCell ()
@property(strong,nonatomic) NSMutableArray *gridArr;

@end

@implementation DataGridCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)refreshData:(NSArray *)data{
    GridBorder border = GridBorderNone;
    uint64_t gap = abs((int)(data.count-self.gridArr.count));
    if (self.gridArr.count<data.count) {
        for (uint64_t count=0; count<gap; count++) {
            UIView *grid = [UIView createGrid:border
                            backgroundColor:WHITE_COLOR
                                 titleColor:TEXT_COLOR_LIGHT
                                  titleSize:14
                                     origin:CGPointMake(self.gridArr.count*(CELL_WIDTH_SMALL-1), 0)
                                       size:CGSizeMake(CELL_WIDTH_SMALL, CELL_HEIGHT_BIG)];
            grid.tag =  self.gridArr.count + 1;
            [self.gridArr addObject:grid];
            [self.contentView addSubview:grid];
        }
    }
    if (self.gridArr.count>data.count) {
        for (uint64_t count=0; count<gap; count++) {
            UIView *grid = [self.contentView viewWithTag:self.gridArr.count];
            [grid removeFromSuperview];
            [self.gridArr removeObjectAtIndex:self.gridArr.count-1];
        }
    }
    
    for (uint64_t index=0; index<data.count; index++) {
        NSDictionary *valDic = data[index];
        UIView *grid = self.gridArr[index];
        GridBorder border = GridBorderLeft | GridBorderTop;
        if (index==data.count-1) {
            border |= GridBorderRight;
        }
        if (self.isBottom) {
            border |= GridBorderBottom;
        }
        [UIView refreshBorder:border forGrid:grid];
        UILabel *l = [grid viewWithTag:INNERLABEL_TAG];
        [l setTextColor:COLOR_NORMAL];
        l.text = valDic[@"val"];
        BOOL flag = valDic[@"flag"];
        if (flag) {
            if ([l.text isEqualToString:@"阴性"]) {
                [l setTextColor:COLOR_NORMAL];
            }
            else if([l.text isEqualToString:@"阳性"]){
                [l setTextColor:COLOR_ABNORMAL];
            }
            else {
                NSNumberFormatter *formatter = [[NSNumberFormatter alloc]init];
                [formatter setNumberStyle:NSNumberFormatterDecimalStyle];
                double val = [[formatter numberFromString:l.text] doubleValue];
                DataRef *ref = valDic[@"ref"];
                if (ref.min) {
                    double min = 0;
                    min = [ref.min doubleValue];
                    if (val<min) {
                        [l setTextColor:COLOR_ABNORMAL];
                    }
                }
                if (ref.max) {
                    double max = 0;
                    max = [ref.max doubleValue];
                    if (val>max) {
                        [l setTextColor:COLOR_ABNORMAL];
                    }
                }
                //如果没有参考值，则默认为正常范围
                if (!ref.min && !ref.max) {
                    [l setTextColor:COLOR_DEFAULT];
                }
            }
            
        }
    }
}

- (NSMutableArray *)gridArr{
    if (!_gridArr) {
        _gridArr = [NSMutableArray array];
    }
    return _gridArr;
}

@end
